package com.design_mode;

import java.util.logging.Handler;

/**
 * Created by dustaple on 8/2/14.
 */
public class ChainOfResponsibility {
    public static void main(String[] args) {
        Worker worker1 = new Worker(BasicHandler.CAPTAIN, "请假1天");
        Worker worker2 = new Worker(BasicHandler.MANAGER, "请假3天");
        Worker worker3 = new Worker(BasicHandler.BOSS, "请假7天");

        BasicHandler handlerCaptain = new Captain();
        BasicHandler handlerManager = new Manager();
        BasicHandler handlerBoss = new Boss();
        handlerCaptain.setSuperior(handlerManager);
        handlerManager.setSuperior(handlerBoss);

        handlerCaptain.doHandle(worker1);
        handlerCaptain.doHandle(worker2);
        handlerCaptain.doHandle(worker3);
    }
}

class Worker {
    private int applyType;
    private String apply;

    Worker(int applyType, String apply) {
        this.applyType = applyType;
        this.apply = apply;
    }

    public int getApplyType() {
        return applyType;
    }

    public String getApply() {
        return apply;
    }
}

abstract class BasicHandler {
    public static final int CAPTAIN = 1;
    public static final int MANAGER = 2;
    public static final int BOSS = 3;

    private BasicHandler superior;
    private int position;

    public BasicHandler(int position) {
        this.position = position;
    }

    public void setSuperior(BasicHandler handler) {
        this.superior = handler;
    }

//    public void setPosition(int position) {
//        this.position = position;
//    }

    public final void doHandle(Worker worker) {
        if (position == worker.getApplyType()) {
            System.out.println(worker.getApply());
            response(worker);
        } else {
            superior.doHandle(worker);
        }
    }

    public abstract void response(Worker worker);
}

class Captain extends BasicHandler {
    public Captain() {
        //setPosition(BasicHandler.CAPTAIN);
        super(BasicHandler.CAPTAIN);
    }

    @Override
    public void response(Worker worker) {
        System.out.println("同意请假1天.");
    }
}

class Manager extends BasicHandler {
    public Manager() {
        super(BasicHandler.MANAGER);
    }

    @Override
    public void response(Worker worker) {
        System.out.println("同意请假3天.");
    }
}

class Boss extends BasicHandler {
    public Boss() {
        super(BasicHandler.BOSS);
    }

    @Override
    public void response(Worker worker) {
        System.out.println("同意请假7天.");
    }
}